﻿using Spider.Entitys.ErrorModel;
using SpiderCore.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpiderCore.Loggers
{
    public class SpiderMongoDBLog : ISpiderLogger
    {
        IDB db = DbManager.InstanceMongoDB();

        /// <summary>
        /// 记录异常类型
        /// </summary>
        private readonly Type _type;
        public SpiderMongoDBLog(Type type)
        {
            this._type = type;
        }

        /// <summary>
        /// 调试
        /// </summary>
        /// <param name="message"></param>
        public void Debug(object message, string address = "")
        {
            Save(message, address, 6);
        }

        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="message"></param>
        public void Error(object message, string address = "")
        {
            Save(message, address, 2);
        }

        /// <summary>
        /// 致命信息
        /// </summary>
        /// <param name="message"></param>
        public void Fatal(object message, string address = "")
        {
            Save(message, address, 1);
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="message"></param>
        public void Info(object message, string address = "")
        {
            //Save(message, address);
            Console.WriteLine(message);
        }

        /// <summary>
        /// 埋点
        /// </summary>
        /// <param name="message"></param>
        public void Point(object message, string address = "")
        {
            Save(message, address, 4);
        }

        /// <summary>
        /// 警告
        /// </summary>
        /// <param name="message"></param>
        public void Warn(object message, string address = "")
        {
            Save(message, address, 3);
        }


        /// <summary>
        /// 保存日志信息
        /// </summary>
        /// <param name="message"></param>
        private void Save(object message, string address, int errorLevel)
        {
            try
            {
                if (message.GetType() == typeof(string))
                {
                    db.Set<ErrorLog>("", new ErrorLog()
                    {
                        depth = 0,
                        ErrorMsg = message.ToString(),
                        InnerException = "",
                        Url = address,
                        ErrorLevel = errorLevel
                    });
                }
                else
                {
                    var exc = message as Exception;
                    db.Set<ErrorLog>("", new ErrorLog()
                    {
                        depth = 0,
                        ErrorMsg = exc.Message,
                        InnerException = exc.InnerException?.Message,
                        Url = address,
                        ErrorLevel = errorLevel
                    });
                }
               
            }
            catch (Exception ex)
            {

            }
        }


    }
}
